﻿using System;
using System.Collections.Generic;
using System.Data;
using Dao;
using Domain.Common;

namespace Domain.Officers
{
    /// <summary>
    /// 文武将
    /// </summary>
    public partial class Officer
    {
        /// <summary>
        /// 所有武将
        /// </summary>
        public static Dictionary<int, Officer> Officers = new Dictionary<int, Officer>();
        /// <summary>
        /// 未登场武将
        /// </summary>
        public static Dictionary<int, Officer> UnOfficeOfficers = new Dictionary<int, Officer>();
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string Name { get; set; }
        public string OtherName { get; set; }
        public bool Sex { get; set; }
        public string PicNumber { get; set; }
        public bool IsLive { get; set; }
        /// <summary>
        /// 登场年份
        /// </summary>
        public short YearAvailable { get; set; }
        public short YearBorn { get; set; }
        public short YearDead { get; set; }
        /// <summary>
        /// 武力
        /// </summary>
        public short Strength { get; set; }
        /// <summary>
        /// 统率
        /// </summary>
        public short Commander { get; set; }
        /// <summary>
        /// 智力
        /// </summary>
        public short Intelligence { get; set; }
        /// <summary>
        /// 政治
        /// </summary>
        public short Politics { get; set; }
        /// <summary>
        /// 魅力
        /// </summary>
        public short Glamour { get; set; }
        /// <summary>
        /// 名望
        /// </summary>
        public int Reputation { get; set; }
        /// <summary>
        ///  武将初始所在城池(归属)。
        /// </summary>
        public short? BuildingId { get; set; }
        /// <summary>
        ///  武将初始登陆城池(在野)。
        /// </summary>
        public short? LoginBuildingId { get; set; }
        /// <summary>
        /// 忠诚度指数
        /// </summary>
        public short LoyaltyIndex { get; set; }
        /// <summary>
        /// 当前状态
        /// </summary>
        public OfficerState State { get; set; }
        /// <summary>
        /// 功绩
        /// </summary>
        public int Performance { get; set; }
        /// <summary>
        /// 武将风格
        /// </summary>
        public OfficerStyle Style = new OfficerStyle();
        /// <summary>
        /// 武将性格
        /// </summary>
        public List<OfficerCharacter> Characters = new List<OfficerCharacter>();
        /// <summary>
        /// 武将才能
        /// </summary>
        public List<OfficerAbility> Abilities = new List<OfficerAbility>();
        /// <summary>
        /// 武将当前从事的内政工作
        /// </summary>
        public WorkType? WorkType { get; set; }
        /// <summary>
        /// 武将登庸信息（工作任务为登庸时才有）
        /// </summary>
        public VisiteOfficer VisiteOfficer;
        /// <summary>
        /// 归属城池（已归属）
        /// </summary>
        public Building BelongBuilding { get; set; }
        /// <summary>
        /// 当前所在城池
        /// </summary>
        public Building InBuilding { get; set; }
        /// <summary>
        /// 武将当前官职
        /// </summary>
        public OfficialPosition OfficialPosition { get; set; }
        /// <summary>
        /// 武将持有的商品宝物
        /// </summary>
        public List<Treasure> HasTreasures = new List<Treasure>();

        public static void Load()
        {
            if (Officers.Count > 0)
                return;
            DataTable tb = OfficerDao.SelectAllBuilding("194_QXGJ.s3db");
            for (int i = 0; i < tb.Rows.Count; i++)
            {
                DataRow dr = tb.Rows[i];
                Officer s = new Officer();
                s.Id = Convert.ToInt32(dr["Id"]);
                if (dr["LoginBuildingId"] != DBNull.Value)
                    s.LoginBuildingId = Convert.ToInt16(dr["LoginBuildingId"]);
                if (dr["BuildingId"] != DBNull.Value)
                s.BuildingId = Convert.ToInt16(dr["BuildingId"]);
                s.FirstName = dr["FirstName"].ToString();
                s.Name = dr["Name"].ToString();
                s.OtherName = dr["OtherName"].ToString();
                s.Sex = Convert.ToBoolean(dr["Sex"]);
                s.PicNumber = dr["PicNumber"].ToString();
                s.IsLive = Convert.ToBoolean(dr["IsLive"]);
                s.YearAvailable = Convert.ToInt16(dr["YearAvailable"]);
                s.YearBorn = Convert.ToInt16(dr["YearBorn"]);
                s.YearDead = Convert.ToInt16(dr["YearDead"]);
                s.Strength = Convert.ToInt16(dr["Strength"]);
                s.Commander = Convert.ToInt16(dr["Commander"]);
                s.Intelligence = Convert.ToInt16(dr["Intelligence"]);
                s.Politics = Convert.ToInt16(dr["Politics"]);
                s.Glamour = Convert.ToInt16(dr["Glamour"]);
                s.Reputation = Convert.ToInt32(dr["Reputation"]);
                s.LoyaltyIndex = 100;
                Officers.Add(s.Id, s);
            }
        }
        public static void Init()
        {
            foreach (KeyValuePair<int, Officer> pair in Officers)
            {
                //设置武将所属城池
                if (pair.Value.BuildingId != null)
                {
                    Building building = Building.Buildings[(short) pair.Value.BuildingId];
                    pair.Value.BelongBuilding = building;
                    pair.Value.State = OfficerState.OnOffice;
                    pair.Value.InBuilding = building;
                    building.OwnedOfficers.Add(pair.Value);
                    building.InBuildingOfficers.Add(pair.Value);
                }
                //设置登场武将
                else if (pair.Value.YearAvailable >= HistoryDate.CurrentDate.Year && pair.Value.LoginBuildingId != null)
                {
                    pair.Value.State = OfficerState.OutOffice;
                    Building building = Building.Buildings[(short) pair.Value.LoginBuildingId];
                    building.InBuildingOfficers.Add(pair.Value);
                    pair.Value.InBuilding = building;
                }
                //将未登场武将收集起来
                else if (pair.Value.LoginBuildingId != null)
                {
                    UnOfficeOfficers.Add(pair.Value.Id,pair.Value);
                }
            }
            //装配商品宝物。一个武将可以有多个相同的商品宝物
            DataTable tb = OfficerHasTreasureDao.SelectAllOfficerTreasure("194_QXGJ.s3db");
            for (int i = 0; i < tb.Rows.Count; i++)
            {
                DataRow dr = tb.Rows[i];
                short officerId = Convert.ToInt16(dr["OfficerId"]);
                int treasureId = Convert.ToInt16(dr["TreasureId"]);
                Officer officer = Officers[officerId];
                Treasure treasure = Treasure.Treasures[treasureId];
                officer.HasTreasures.Add(treasure);
            }
        }
    }
}
